package com.eelye.util.net;


import org.apache.commons.lang3.StringUtils;

public class IpTool {

    public static int ip2Int(String ipStr) {
        if (StringUtils.isBlank(ipStr)) {
            throw new RuntimeException("param ipStr can not be blank.");
        }

        String[] ip = ipStr.split("\\.");
        return (Integer.valueOf(ip[0]) << 24) + (Integer.valueOf(ip[1]) << 16)
                + (Integer.valueOf(ip[2]) << 8) + Integer.valueOf(ip[3]);
    }

    public static String int2Ip(int ipInt){
        StringBuilder ip = new StringBuilder();
        ip.append(ipInt >>> 24).append('.')
                .append( ipInt >>> 16 & 0xFF).append('.')
                .append(ipInt >>> 8 & 0xFF).append('.')
                .append(ipInt & 0xFF);
        return ip.toString();
    }
}
